package com.learning.optimize.jdk.regex;

import lombok.Data;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

// 使用record定义不可变数据类（Java 16+）
@Data
public class MatchResult {

   private String fullMatch;
   private String codePrefix;
   private String questionType
}

@Data
public class AdvancedResult{
    private String prefix;
    private String questionType;
    private String suggestedResponse;
    private boolean isValid;
}

public class ModernCareerMatcher {
    private final Pattern groupPattern;
    private final Pattern extendedPattern;

    public ModernCareerMatcher() {
        groupPattern = Pattern.compile("\\[(\\w+)\\](的职业代码|是几类职业|职业代码是多少)");
        extendedPattern = Pattern.compile("\\[(zhiyecc\\d+|code|career_code|job_code)\\](的职业代码|是几类职业|职业代码是多少)");
    }

    /**
     * 使用Optional进行更安全的匹配
     */
    public Optional<MatchResult> extractGroupsModern(String text) {
        return groupPattern.matcher(text)
                .results()
                .findFirst()
                .map(match -> new MatchResult(
                        match.group(0),
                        match.group(1),
                        match.group(2)
                ));
    }

    /**
     * 使用Stream进行批量处理
     */
    public List<MatchResult> extractAllMatches(List<String> texts) {
        return texts.stream()
                .map(this::extractGroupsModern)
                .filter(Optional::isPresent)
                .map(Optional::get)
                .collect(Collectors.toList());
    }

    /**
     * 进阶匹配使用record
     */
    public Optional<AdvancedResult> validateAndExtractModern(String text) {
        return extendedPattern.matcher(text)
                .results()
                .findFirst()
                .map(match -> {
                    String prefix = match.group(1);
                    String questionType = match.group(2);

                    String response = switch (questionType) {
                        case "的职业代码" -> "获取" + prefix + "的职业编码";
                        case "是几类职业" -> "查询" + prefix + "的职业分类";
                        case "职业代码是多少" -> "查找" + prefix + "的职业代码值";
                        default -> "未知查询类型";
                    };

                    return new AdvancedResult(prefix, questionType, response, true);
                });
    }

    public static void main(String[] args) {
        ModernCareerMatcher modernMatcher = new ModernCareerMatcher();

        List<String> testStrings = Arrays.asList(
                "[zhiyecc1]的职业代码",
                "[zhiyecc2]是几类职业",
                "[code]职业代码是多少",
                "[invalid]的文本"
        );

        System.out.println("=== 现代化匹配结果 ===");
        testStrings.forEach(text -> {
            modernMatcher.extractGroupsModern(text)
                    .ifPresentOrElse(
                            result -> System.out.println("匹配成功: " + result),
                            () -> System.out.println("不匹配: " + text)
                    );
        });

        System.out.println("\n=== 进阶现代化匹配 ===");
        testStrings.forEach(text -> {
            modernMatcher.validateAndExtractModern(text)
                    .ifPresentOrElse(
                            result -> System.out.println("进阶匹配: " + result),
                            () -> System.out.println("进阶不匹配: " + text)
                    );
        });
    }
}